﻿/// <reference path="../jquery-1.7.2-vsdoc.js" />

function requestFullscreen(element) {
    if (element.requestFullscreen) {
        element.requestFullscreen();
    } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
    } else if (element.webkitRequestFullScreen) {
        element.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
    } else {
        alert("Fullscreen not supported!");
    }
}

function closeFullscren() {
    if (document.cancelFullScreen) {
        document.cancelFullScreen();
    } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if (document.webkitCancelFullScreen) {
        document.webkitCancelFullScreen();
    }

}

var $instructionsDialog = $('<div></div>').dialog({
    autoOpen: false,
    modal: true,
    title: 'Instructions',
    resizable: false,
    height: 600,
    width: 500,
    buttons: {
        Ok: function () {
            fullScreen();
            $(this).dialog("close");
        }
    }
});

var $endingDialog = $('<div></div>').dialog({
    autoOpen: false,
    modal: true,
    resizable: false,
    height: 600,
    width: 500,
    close: function () {
        $('#jsonResult').val(JSON.stringify(trials));
        $('form').submit();
    },
    buttons: {
        Ok: function () {
            $(this).dialog("close");
        }
    }
});



var currentTrial = -1;

$(document).ready(function () {
    center();

    $(window).resize(function () {
        center();
    });

    $instructionsDialog.html('Space to continue');
    $instructionsDialog.dialog('open');

    $(window).keypress(function (key) {
        // space key to start
        if (currentTrial == -1) {
            if (key.which != 32)
                return;
            startExercise();
        }
    });
});

function startExercise() {
    $('.photoDisplay').show();
    showNextTrial();
}


var trialStartTime = 0;
var picSelector = $('#picSeparator');
var crtNeutralPhoto = '';

function showNextTrial() {
    if (currentTrial == trials.length - 1) {
        exitExercise();
    }

    if (currentTrial != - 1) {
        trials[currentTrial].Reaction = new Date().getTime() - trialStartTime;
        trials[currentTrial].NeutralPhoto = crtNeutralPhoto;
    }

    currentTrial++;

    var i = 0;
    var photoIndex = 0;
    var type = 0;
    $('#matrixTable').find('tr').each(function () {
        $(this).find('td').each(function () {
            var photo = trials[currentTrial].photos[i++];
            if (photo.PhotoType == 0) {
                crtNeutralPhoto = neutralPhotos[photo.PhotoIndex];
                $(this).find('img').attr('src', '/uploads/neutral/' + crtNeutralPhoto);
                $(this).find('img').attr('onclick', 'showNextTrial();');
            } else if (photo.PhotoType == 1) {
                $(this).find('img').attr('src', '/uploads/negative/' + negativePhotos[photo.PhotoIndex]);
                $(this).find('img').removeAttr('onclick');
            }
        });
    });

    trialStartTime = new Date().getTime();
}

function exitExercise() {
    closeFullscren();
    $endingDialog.html('Thank you for your time!');
    $endingDialog.dialog('open');
}


function center() {
    $('.photoDisplay').css({
        position: 'absolute',
        left: ($(window).width() - $('.photoDisplay').outerWidth()) / 2,
        top: ($(window).height() - $('.photoDisplay').outerHeight()) / 2
    });
}


function fullScreen() {
    requestFullscreen(document.getElementById("container"));
}
